﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AusProject.OrderDataControls;

namespace AusProject
{
    public partial class FormSupplierOrderRuntime : Form
    {
        public FormSupplierOrderRuntime()
        {
            InitializeComponent();
        }

        private SupplierOrderFormInfo formInfo = null;

    
        public void LoadForm(SupplierOrderFormInfo formInfo, bool isSavedForm)
        {
            this.formInfo = formInfo;

            panelOrderForm.Width = formInfo.Width;
            panelOrderForm.Height = formInfo.Height;

            try
            {
                panelOrderForm.BackgroundImage = Image.FromFile(formInfo.BackgroundImagePath);
            }
            catch
            {
                //TODO: Bao loi
            }

            panelOrderForm.Controls.Clear();
            foreach (DataBoxInfo dataBoxInfo in formInfo.ListOfDataBox)
            {
                DataBox dataBox = dataBoxInfo.CreateDataBox();
                dataBox.Mode = DataControlMode.Running;
                if (!isSavedForm)
                {
                    dataBox.Value = dataBox.DefaultValue;
                }
                panelOrderForm.Controls.Add(dataBox);
            }

            this.Invalidate();
        }



        private bool accepted = false;

        public bool Accepted
        {
            get
            {
                return accepted;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            accepted = true;
            this.Close();
        }

        private void btnDiscard_Click(object sender, EventArgs e)
        {
            accepted = false;
            this.Close();
        }


        public SupplierOrderFormInfo ExportSupplierOrderFormInfo()
        {
            List<DataBoxInfo> listOfDataBox = new List<DataBoxInfo>();
            foreach (DataBox databox in panelOrderForm.Controls)
            {
                listOfDataBox.Add(databox.ExportDataBoxInfo());
            }

            return new SupplierOrderFormInfo(formInfo.Title, formInfo.Width, formInfo.Height, formInfo.BackgroundImagePath, listOfDataBox);
        }


        public void SetTitle(string formTitle, int formID)
        {
            string oFormTitle = string.IsNullOrEmpty(formTitle) ? "" : " - " + formTitle;
            string oFormID = formID >= 0 ? " (#" + formID + ")" : " (NoID)";
            this.Text = "Supplier Order Form" + oFormTitle + oFormID;
        }
    }
}
